<?php
namespace app\admin\controller\center;
use app\admin\model\AuthRule;
use app\admin\controller\Base;
use app\common\util\DBUtil;
use app\common\util\GameUtil;
use think\Validate;
use app\admin\model\Platform;
use app\admin\model\Menu;
/**
* 进行权限管理
*/
class Admin extends Base{
    

    public function admin_list(){       

        // 如果是admin的管理员，可以看到所有管理员，如果是非Admin，只能看到本平台的
    }


    public function user_list() {          
        $db = DBUtil::connect_admin();       
        $userList = $db->removeOption()->table('admin_user')->paginate(6);
        $groupList = $db->removeOption()->table('admin_group')->select();
        $groupPlatform = $db->removeOption()->table('admin_group_platform')->select();        
        $groupDict = GameUtil::list2Dict($groupList, 'groupid');

        $count = $userList->total();
        foreach($userList as $index =>$user) {
            if(array_key_exists($user['groupid'], $groupDict)){                
                $group = $groupDict[$user['groupid']];
                if($group) {
                    $user['group_title'] = $group['title'];                       
                    $userList[$index] = $user;           
                }
            }
        }    
        $page = $userList->render(); 
        $this->assign('users', $userList);
        $this->assign("page", $page);
        return $this->fetch();
    }


    public function user_edit(){
        $userid = input('uid');
        $db = DBUtil::connect_admin();
        $groupList = $db->removeOption()->table('admin_group')->select();
        $this->assign("groups", $groupList);
        if(request()->isAjax()){
            $user = $db->removeOption()->table('admin_user')->where('id', $userid)->find();
            if($user) {
                $isDisabled = !$user['is_disabled'];
                $data = array("is_disabled"=> $isDisabled);
                $db->removeOption()->table('admin_user')->where("id", $userid)->update($data);
                if($isDisabled) {
                    $this->success("状态禁止");
                } else {
                    $this->success("修改成功");
                }
            } else {
                $this->error("非法的账号ID");
            }

        } else if(request()->isPost()){                
            $validate = Validate::make([
                'username|登陆用户名'  => 'require|max:50|min:1',
                'groupid|用户组'       => 'require',
            ]);          

            $newData = GameUtil::getInputParamArray(['username', 'password', 'groupid', 'is_disabled']);

            if(!$newData['password']) {
                unset($newData['password']);
            }
            if(!isset($newData['is_disabled'])){
                $newData['is_disabled'] = 1;
            }
            # 只有超级账号才能更改用户组,
            if($userid && $userid > 0) {                     
                $newData['id'] = $userid;                          
                $db->removeOption()->table('admin_user')->update($newData);
            } else {
                $checkUser = $db->removeOption()->table('admin_user')->where('username', $newData['username'])->find();
                if($checkUser) {
                    $this->error("账号已经存在, 不能重复增加", url('admin/center.admin/user_list'));
                }
                $newData['addtime'] = date('Y-m-d h:i:s',time());;
                $db->removeOption()->table('admin_user')->insert($newData);
            }                      
            $this->success("更新成功", url('admin/center.admin/user_list'));

        } else {

            if($userid) {                
                $user = $db->removeOption()->table('admin_user')->where('id', $userid)->find();
                $this->assign("user", $user);
            } 
            return $this->fetch();
        }
    }
    public function user_del(){
        $userid = input('uid');
        if($userid && $userid > 0){
            DBUtil::connect_admin()->table('admin_user')->where('id', $userid)->delete();
        } else {
            $this->error('非法的编号', url('admin/center.admin/user_list'));
        }

        $this->success("删除成功", url('admin/center.admin/user_list'));
    }

    public function group_list(){
        $db = DBUtil::connect_admin();
        $groupList = $db->table('admin_group')->paginate(10);        
        $this->assign('groups', $groupList);        
        $this->assign('page',$groupList->render());
        return $this->fetch();
    }

    public function group_edit(){
        $groupid = input('groupid');
       
        $db = DBUtil::connect_admin();
        if(request()->isAjax()){
            // 只能是更新
            // var_dump("ajax...........");
            $groupid = input("groupid");
            if($groupid == 1){
                $this->error("不能对超级管理员进行操作");
            }
            $group = $db->table('admin_group')->where('groupid', $groupid)->find();
            if($group) {
                $isDisabled = !$group['is_disabled'];
                $data = array("is_disabled"=> $isDisabled);
                $db->removeOption()->table('admin_group')->where("groupid", $groupid)->update($data);
                if($isDisabled) {
                    $this->success("状态禁止");
                } else {
                    $this->success("修改成功");

                }
            } else {
                $this->error("非法的组ID");
            }

        } else if(request()->isPost()){    
            $newData = GameUtil::getInputParamArray(['title']);
            if(!isset($newData['title'])) {
                $this->error('用户组不能为空', url('admin/center.admin/group_list'));
            }
            $ruleList = input('rules/a');            
            if(!$ruleList) {
                $this->error('权限组不能为空', url('admin/center.admin/group_list'));   
            }
            $platforms = input('platforms/a');

            $rules = implode(",", $ruleList);
            $newData['rules'] = $rules;
            $copy = input('copy');

            if(!$copy && $groupid && $groupid > 0) {      
                $newData['groupid'] = $groupid;         
                $db->removeOption()->table('admin_group')->update($newData);
            } else {
                $groupid = $db->removeOption()->table('admin_group')->insertGetId($newData);
                
            }

            if($platforms) {
                $data = array();
                foreach($platforms as  $platform){
                    $data[] = array('platform'=> $platform, 'groupid'=> $groupid);
                }
                $db->removeOption()->table('admin_group_platform')->where('groupid', $groupid)->delete();
                $db->removeOption()->table('admin_group_platform')->insertAll($data);
            
            }

            
            $this->success('修改成功', url('admin/center.admin/group_list'));

        } else {

            $platforms = $db->removeOption()->table('platform')->select();

            $centerMenu = Menu::getCenterMenu();
            $gameMenu = Menu::getGameMenu();
            $this->assign("center_menus", $centerMenu);
            $this->assign('game_menus', $gameMenu);
            

            if($groupid) {              
                $group = $db->removeOption()->table('admin_group')->where('groupid', $groupid)->find();
                $copy = input('copy');
                if($copy && $group) { // copy新增
                    unset($group['groupid']);
                    unset($group['title']);
                }                             
                $groupPlatfromDict = GameUtil::list2Dict($db->removeOption()->table('admin_group_platform')->where('groupid', $groupid)->select(), 'platform');

                foreach($platforms as $key => $platform) {
                    if(isset($groupPlatfromDict[$platform['platform']])) {
                        $platform['check'] = 1;
                        $platforms[$key] = $platform;
                    }               
                }                               

                $this->assign('group', $group);
            }  

            $this->assign('platforms', $platforms);
            return $this->fetch();
        }
    }

    public function group_del(){
        $groupid = input('groupid');
        $db = DBUtil::connect_admin();
        $db->table('admin_group')->where('groupid', $groupid)->delete();
        $this->success("删除成功", url('admin/center.admin/group_list'));
    }

    public function platform_list(){
        // $platform = new Platform;
        // $platforms = $platform->get_all_platform();
        $platforms = DBUtil::connect_admin()->table('platform')->paginate(50);
        $this->assign('platforms',$platforms);
        $this->assign('page', $platforms->render());
        return $this->fetch();
    }
   
    /**
    * 平台编辑,只有Admin管理员才有权限
    */
    public function platform_edit(){
        $platform = input('platform');
        if(request()->isAjax()){
            $db = DBUtil::connect_admin();
            $platformData = $db->removeOption()->table('platform')->where('platform', $platform)->find();
            if($platformData) {
                $isDisabled = !$platformData['is_disabled'];
                $data = array("is_disabled"=> $isDisabled);
                $db->removeOption()->table('platform')->where("platform", $platform)->update($data);
                if($isDisabled) {
                    $this->success("状态禁止");
                } else {
                    $this->success("修改成功");
                }
            } else {
                $this->error("非法的账号ID");
            }
        }else if(request()->isPost()) {
            $validate = Validate::make([
                'platform|平台'  => 'require|max:50|min:1',
                'title|描述'     => 'require|max:50|min:1',
                'ip|IP地址'      => 'require|ip',
                'port|端口'      =>  'require|between:1024,65535',
                // 'is_disabled|是否启用'=> 'require|in:0,1'
            ]);                        
            $rst   = $validate->check(request()->param());
            if(true != $rst) {                
                $this->error($validate->getError(), url('admin/center.admin/platform_list'));
            }   
            $db = DBUtil::connect_admin();
            $isDisabled = input('?is_disabled') ? 0:1;

            $data = $db->table('platform')->where('platform', $platform)->find();
            $newData = ['platform' => $platform,  'title'=> input('title'), 'ip'=>input('ip'), 'port'=>input('port'),'is_disabled' => $isDisabled
                ];
            if($data) {
                $db->removeOption()->table('platform')->update($newData);
            } else {
                $db->removeOption()->table('platform')->insert($newData);
            }

            $this->success('修改成功', url('admin/center.admin/platform_list'));
        } else {
            if($platform) {
                $db = DBUtil::connect_admin();
                $data = $db->table('platform')->where('platform', $platform)->find();
                $this->assign('platforms', $data);            
            }

            return $this->fetch();
        }
    }
    
    public function platform_del(){
        $this->success("删除成功", url('admin/center.admin/platform_list'));
    }

    
    
    
    // public function index(){

    // }
}